﻿using System;
using System.Collections.Generic;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxPanel;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using olierFrm.DataBase.FrameWork;
using olierFrm.DataBase.FrameWork.StdQuery;

namespace olierFrm.WebFormDev.Plugin
{
    public static class HandelControl
    {
        public static void SetControlValue(object control, object value)
        {

            if (control.GetType().Name.Equals("ASPxDateEdit"))
            {
                ((ASPxDateEdit)control).Text = value.ToString();
            }
            else if (control.GetType().Name.Equals("ASPxComboBox"))
            {
                ((ASPxComboBox)control).Value = value.ToString();
            }
            else
            {
                ((ASPxTextBox)control).Text = value.ToString();
            }

        }

        public static System.Web.UI.Control GenControl(ControlInfo Param)
        {
            if (Param.ParamName.ToUpper().IndexOf("STORENO") != -1)
            {
                var control = new ASPxButtonEdit();
                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;

                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.Style.Add("margin", "3px");

                StdQuerySQL sqs = new StdQuerySQL("GetStore");
                sqs.AddSystemParams();
                var dt = sqs.ExecuteSQL();
                if (dt.Rows.Count > 1)
                {
                    control.ClientSideEvents.ButtonClick = "BtnDialogSel";
                    control.Attributes.Add("url", "Plugin/DialogTreeSel.aspx?QueryName=SelStore");
                    control.Buttons.Add("...");
                }
                else
                {
                    control.Text = dt.Rows[0][0].ToString();
                    control.ReadOnly = true;
                }
                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("LIST") != -1)
            {
                var control = new ASPxComboBox();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;
                //control.Items.Add(" ", "");
                if (Param.Common != null && Param.Common.Count != 0)
                {
                    foreach (var c in Param.Common)
                    {
                        control.Items.Add(c.sComDesc, c.sComID);
                    }
                }
                if (Param.DefaultValue != null && control.Items.FindByTextWithTrim(Param.DefaultValue) != null)
                {
                    control.Items.FindByTextWithTrim(Param.DefaultValue).Selected = true;
                }
                control.Style.Add("margin", "3px");
                return control;
            }

            else if (Param.ParamType.Trim().ToUpper().IndexOf("ENDDATE") != -1)
            {

                var control = new ASPxDateEdit();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;

                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                control.Date = DateTime.Now;
                control.EditFormat = EditFormat.Custom;
                if (Param.DefaultValue == "-30")
                {
                    control.Date = Convert.ToDateTime(DateTime.Now.Year + "-" + (DateTime.Now.Month - 1) + "-01");
                }
                control.Style.Add("margin", "3px");
                control.ASPxDateEditStyle();

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("DATE") != -1)
            {

                var control = new ASPxDateEdit();

                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;

                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                control.Date = DateTime.Now;
                control.EditFormat = EditFormat.Custom;

                try
                {
                    control.Date = DateTime.Now.AddDays(double.Parse(Param.DefaultValue));
                }
                catch
                {
                    control.Date = Param.DefaultValue != null && Param.DefaultValue.Length != 0 ? DateTime.Parse(Param.DefaultValue) : DateTime.Now;
                }
                control.Style.Add("margin", "3px");
                control.ASPxDateEditStyle();
                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().Equals("BOOLEAN"))
            {
                var control = new ASPxCheckBox();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                if (Param.DefaultValue != null && Param.DefaultValue.Length != 0) control.Checked = Convert.ToBoolean(Param.DefaultValue);
                control.Style.Add("margin", "3px");
                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("FLOAT") != -1)
            {
                var control = new ASPxTextBox();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;

                if (!Param.ReadOnly)
                {
                    control.ValidationSettings.SetFocusOnError = true;
                    control.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.None;
                    control.ValidationSettings.RegularExpression.ValidationExpression = @"^(\d+.\d+)|(\d+)$";
                }
                else
                {
                    control.Style.Add("margin", "3px");
                }

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("INTEGER") != -1)
            {
                var control = new ASPxTextBox();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;
                if (!Param.ReadOnly)
                {
                    control.ValidationSettings.SetFocusOnError = true;
                    control.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.None;
                    control.ValidationSettings.RegularExpression.ValidationExpression = @"^\d+$";
                }
                else control.Style.Add("margin", "3px");

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("DIALOG") != -1)
            {
                var control = new ASPxButtonEdit();
                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;

                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.Style.Add("margin", "3px");
                control.ReadOnly = Param.ReadOnly;

                control.ClientSideEvents.ButtonClick = "BtnDialogSmartLookup";
                //control.Attributes.Add("url", "Plugin/DialogTreeSel.aspx?QueryName=SelStore");
                control.Buttons.Add("...");
                //control.Attributes.Add("ContentUrl", ");

                //control.ClientSideEvents.ButtonClick = "function(s, e) {pcSmartLookup.SetContentUrl('/SmartLookupPage.aspx?QueryName=OneOrders');pcSmartLookup.Show();}";
                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("PROPERTY") != -1)
            {
                var control = new ASPxPanel();
                //control.Border.BorderWidth = 1;

                if (Param.Common != null && Param.Common.Count != 0)
                {
                    foreach (var c in Param.Common)
                    {
                        var chk = new ASPxCheckBox()
                        {
                            Checked = Param.DefaultValue != null && ((int.Parse(Param.DefaultValue) & int.Parse(c.sComID)) == int.Parse(c.sComID)) ? true : false,
                            Text = c.sComDesc.Substring(c.sComDesc.IndexOf(".") + 1)
                        };
                        chk.Attributes.Add("chkValue", c.sComID);
                        chk.Style.Add("float", "left");
                        control.Controls.Add(chk);
                        //control.Controls.Add(new LiteralControl("<span>" + c.sComDesc.Substring(c.sComDesc.IndexOf(".") + 1) + "</span>"));
                    }
                    control.Attributes.Add("changeborder", "1");
                    control.Controls.Add(new LiteralControl("<div style='clear:both'></div>"));
                }
                //if (Param.DefaultValue != null && control.Items.FindByTextWithTrim(Param.DefaultValue) != null)
                //{
                //    control.Items.FindByTextWithTrim(Param.DefaultValue).Selected = true;
                //}
                control.Style.Add("margin", "3px");

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("MONTH") != -1)
            {
                DateTime defaultDateTime = DateTime.Now;
                if (Param.DefaultValue != null)
                {
                    defaultDateTime = DateTime.Now.AddMonths(int.Parse(Param.DefaultValue));
                }
                var year = new ASPxSpinEdit();
                year.Width = 53;
                year.Style.Add("float", "left");
                year.MinValue = 1949;
                year.MaxValue = 2199;
                year.MaxLength = 4;
                if (Param.DefaultValue != null) year.Text = defaultDateTime.Year.ToString();
                else year.Text = DateTime.Now.Year.ToString();

                var month = new ASPxSpinEdit();
                month.Width = 40;
                month.Style.Add("float", "left");
                month.MaxValue = 12;
                month.MinValue = 1;
                month.MaxLength = 2;
                if (Param.DefaultValue != null) month.Text = defaultDateTime.Month.ToString();
                else year.Text = DateTime.Now.Month.ToString();

                var control = new ASPxPanel();
                control.Controls.Add(year);
                control.Controls.Add(month);
                control.Controls.Add(new LiteralControl("<div style='clear:both'></div>"));
                control.Style.Add("margin", "3px");

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("WEEK") != -1)
            {
                DateTime defaultDateTime = DateTime.Now;
                if (Param.DefaultValue != null)
                {
                    int outDefaultValue = 0;
                    if (int.TryParse(Param.DefaultValue, out outDefaultValue))
                    {
                        defaultDateTime = DateTime.Now.AddDays((outDefaultValue * 7));
                    }
                }
                var year = new ASPxSpinEdit();
                year.Width = 53;
                year.Style.Add("float", "left");
                year.MinValue = 1949;
                year.MaxValue = 2199;
                year.MaxLength = 4;
                if (Param.DefaultValue != null) year.Text = defaultDateTime.Year.ToString();
                else year.Text = DateTime.Now.Year.ToString();

                var week = new ASPxSpinEdit();
                week.Width = 40;
                week.Style.Add("float", "left");
                week.MaxValue = 53;
                week.MinValue = 1;
                week.MaxLength = 2;
                if (Param.DefaultValue != null) week.Text = (defaultDateTime.DayOfYear / 7).ToString();
                else year.Text = DateTime.Now.Month.ToString();

                var control = new ASPxPanel();
                control.Controls.Add(year);
                control.Controls.Add(week);
                control.Controls.Add(new LiteralControl("<div style='clear:both'></div>"));
                control.Style.Add("margin", "3px");

                return control;
            }
            else if (Param.ParamType.Trim().ToUpper().IndexOf("STDQUERY") != -1)
            {
                var control = new ASPxButtonEdit();
                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;

                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.Style.Add("margin", "3px");

                control.ClientSideEvents.ButtonClick = "BtnDialogSel";
                control.Attributes.Add("url", "Plugin/DialogGridSel.aspx?QueryName=" + Param.SQL + "&CallBackField=" + Param.ParamName);
                control.Buttons.Add("...");

                return control;
            }
            else
            {//if (Param.ParamType.Trim().ToUpper().IndexOf("STRING") != -1)

                var control = new ASPxTextBox();
                if (Param.ClientInstanceName != null && Param.ClientInstanceName.Length != 0)
                    control.ClientInstanceName = Param.ClientInstanceName;
                if (Param.Width != null) control.Width = Unit.Parse(Param.Width);
                else control.Width = 100;
                if (Param.ParamName != null) control.ID = Param.ParamName;
                control.ReadOnly = Param.ReadOnly;

                if (Param.DefaultValue != null) control.Text = Param.DefaultValue;
                control.Style.Add("margin", "3px");
                return control;
            }
        }


        public static object GetControlValue(object control)
        {
            if (control.GetType().Name == "ASPxButtonEdit")
                return ((ASPxButtonEdit)control).Text;


            if (control.GetType().Name == "ASPxComboBox")
                return ((ASPxComboBox)control).SelectedItem.Value;

            else if (control.GetType().Name == "ASPxDateEdit")
                return ((ASPxDateEdit)control).Text;

            else if (control.GetType().Name == "ASPxButtonEdit")
                return ((ASPxButtonEdit)control).Text;

            else if (control.GetType().Name == "ASPxCheckBox")
                return ((ASPxCheckBox)control).Value;

            else //(control.GetType().Name == "ASPxTextBox")
                return ((ASPxTextBox)control).Text;
        }


        /// <summary>
        /// 创建控件DIV，包括标题和控件
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static ASPxPanel CreateField(ControlInfo ci)
        {
            var panel = new DevExpress.Web.ASPxPanel.ASPxPanel();
            panel.ID = "Field_" + ci.ParamName;
            panel.Style.Add("float", "left");
            panel.Attributes.Add("ParamType", ci.ParamType);

            //panel.Style.Add("margin-left", "10px");
            //panel.Style.Add("margin-bottom", "10px");
            var control = HandelControl.GenControl(new ControlInfo()
            {
                ParamName = ci.ParamName,
                ParamType = ci.ParamType,
                Common = ci.Common,
                DefaultValue = ci.DefaultValue,
                SQL = ci.SQL,
                Width = ci.Width,
                ReadOnly = ci.ReadOnly,
                ClientInstanceName = ci.ClientInstanceName,
                //ClientSideEvents = ci.ClientSideEvents
            });
            //control.ID = ci.ParamName;

            //control.ID = ci.ParamName;
            var hgc = new HtmlGenericControl();
            //hgc.Style.Add("padding-bottom", "-5px");
            hgc.InnerHtml = ci.DisplayLabel;



            panel.Controls.Add(hgc);



            //panel.Controls.Add(control);
            if (ci.ParamType == "BOOLEAN")
            {
                panel.Controls.Add(new System.Web.UI.LiteralControl("<br/>"));
                panel.Height = 43;
            }
            panel.Controls.Add(control);

            return panel;
        }

        /// <summary>
        /// 创建控件DIV，包括标题和控件
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static ASPxPanel CreateFieldDataObject(ControlInfo ci)
        {
            var panel = new DevExpress.Web.ASPxPanel.ASPxPanel();
            panel.ID = "Field_" + ci.ParamName;
            panel.Style.Add("float", "left");
            panel.Attributes.Add("ParamType", ci.ParamType);

            //panel.Style.Add("margin-left", "10px");
            //panel.Style.Add("margin-bottom", "10px");
            var control = HandelControl.GenControl(new ControlInfo()
            {
                ParamName = ci.ParamName,
                ParamType = ci.ParamType,
                Common = ci.Common,
                DefaultValue = ci.DefaultValue,
                SQL = ci.SQL,
                Width = ci.Width,
                ReadOnly = ci.ReadOnly,
                ClientInstanceName = ci.ClientInstanceName,
                //ClientSideEvents = ci.ClientSideEvents
            });
            //control.ID = ci.ParamName;

            //control.ID = ci.ParamName;
            var hgc = new HtmlGenericControl();
            //hgc.Style.Add("padding-bottom", "-5px");
            hgc.InnerHtml = ci.DisplayLabel;



            panel.Controls.Add(hgc);
            if (ci.ParamType == "BOOLEAN")
            {
                panel.Controls.Add(new HtmlGenericControl("br"));
                panel.Height = 43;
            }
            panel.Controls.Add(control);
            return panel;
        }
    }


    public class ControlInfo
    {
        //public string ParamID { get; set; }
        public string SQL { get; set; }
        public string DisplayLabel { get; set; }
        public string DefaultValue { get; set; }
        public List<WS_CommonItems> Common { get; set; }
        public string ParamType { get; set; }
        public string ParamName { get; set; }
        public string Width { get; set; }
        public bool ReadOnly { get; set; }
        public string ClientInstanceName { get; set; }
        //public object ClientSideEvents { get; set; }
    }
}
